package com.miaoji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miaoji.domain.dto.Result;
import com.miaoji.domain.po.Cloth;
import com.miaoji.domain.po.ClothCategory;
import com.miaoji.mapper.ClothCategoryMapper;
import com.miaoji.mapper.ClothMapper;
import com.miaoji.service.ClothService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ClothServiceImpl implements ClothService {
    @Autowired
    ClothMapper clothMapper;
    @Autowired
    ClothCategoryMapper categoryMapper;
    @Override
    public List ClothSelect() {
        //Mapper mapper = new Mapper();
        Page<Cloth> page = new Page<>(1, 10);//number为页数,查询第number页
        page.addOrder(OrderItem.desc("update_time")); // 按 updateTime 降序排列
        List<Cloth> clothList = clothMapper.selectPage(page, null).getRecords(); // 获取当前页的数据列表;
        return clothList;
    }
    @Override
    public List ClothSelect(int number) {
        // 如果 number 为非正数，设定默认值为1
        //+默认按着updateTime降序排列
        if (number <= 0) {
            number = 1;
        }
        //Mapper mapper = new Mapper();
        Page<Cloth> page = new Page<>(number, 10);//number为页数,查询第number页
        page.addOrder(OrderItem.desc("update_time")); // 按 updateTime 降序排列
        List<Cloth> clothList = clothMapper.selectPage(page, null).getRecords(); // 获取当前页的数据列表;
        return clothList;
    }

    @Override
    public List<Cloth> ClothSelect(Cloth cloth) {
        QueryWrapper<Cloth> clothQueryWrapper = new QueryWrapper<>();
        if (cloth.getClothName()!=null && !cloth.getClothName().isEmpty()){
            clothQueryWrapper.like("cloth_name", cloth.getClothName());
        }
        if (cloth.getBrand()!=null && !cloth.getBrand().isEmpty()){
            clothQueryWrapper.like("brand", cloth.getBrand());
        }
        if (cloth.getClothCategoryId()!=null){
            clothQueryWrapper.eq("cloth_category", cloth.getClothCategoryId());
        }
        if (cloth.getState()!=null){
            clothQueryWrapper.eq("state", cloth.getState());
        }
        if (cloth.getUpdateTime()!=null){
            clothQueryWrapper.between("update_time", cloth.getUpdateTime(), LocalDateTime.now());
        }
        return clothMapper.selectList(clothQueryWrapper);
    }

    @Override
    public Integer ClothInsert(Cloth cloth) {
        int insert = clothMapper.insert(cloth);
        Integer i = null;
        if (insert>0){
            Result.ok("插入成功");
            i = clothMapper.clothReturn(cloth.getClothName());
            Integer clothCategoryId = cloth.getClothCategoryId();
            String sex=clothCategoryId==1?"男":"女";
            ClothCategory category=new ClothCategory();
            category.setClothId(i);
            category.setCategoryTre(sex);
            categoryMapper.insert(category);
        }
        else {
            System.out.println(Result.fail("插入失败"));
        }

        return i;
    }
//todoCloth cloth
    @Override
    public void ClothUpdate(Cloth cloth) {
        clothMapper.updateById(cloth);
    }

    @Override
    public void ClothDelete(Cloth cloth) {
        clothMapper.deleteById(cloth.getId());
    }

    @Override
    public List ClothOrder(String sort ) {
        // 先不用这个功能了
        return null;
    }
}
